<template>
  <music-list :songs="songs" :title="title" :bgImage="bgImage"></music-list>
</template>
<script>

import musicList from 'components/music-list/music-list'
import { mapGetters } from 'vuex'
import { getSongList } from 'api/recommend'
import { ERR_OK } from 'api/config'
import {createSong} from 'common/js/song'

export default {
  data() {
    return {
      songs: []
    }
  },
  computed: {
    title() {
      return this.disc.dissname
    },
    bgImage() {
      return this.disc.imgurl
    },
    ...mapGetters([
      'disc'
    ])
  },
  created() {
    this._getSongList()
  },
  methods: {
    _getSongList() {
      if(!this.disc.dissid) {
        this.$router.push('/recommend')
      }
      getSongList(this.disc.dissid).then((res) => {
        if (res.code === ERR_OK) {
          this.songs = this.normalizeSongs(res.cdlist[0].songlist)
        }
      })
    },
    // 对歌单列表数据进行处理
    normalizeSongs(list) {
      let ret = []
      list.forEach((musicData) => {
        if(musicData.songid && musicData.albumid) {
          ret.push(createSong(musicData))
        }
      })
      return ret
    }
  },
  components: {
    musicList
  }
}
</script>
<style lang="stylus" scoped>
</style>
